conj
, assoc
, dissoc
Clojure provides several powerful functions to manipulate collections such as lists, vectors, maps, and sets. Three of the most commonly used functions for modifying collections are conj
, assoc
, and dissoc
. These functions are all designed to work in an immutable way, meaning they don't modify the original collection but instead return a new modified collection.
Let's go over each function with examples:
conj
(Add to collection)The conj
function is used to add elements to a collection. The behavior of conj
depends on the type of collection it is applied to:
conj
with Lists, Vectors, and Sets; Using conj with a List
(def my-list '(2 3 4))
(def new-list (conj my-list 1))
; my-list is unchanged, new-list is (1 2 3 4)
(println new-list)
; Using conj with a Vector
(def my-vector [1 2 3])
(def new-vector (conj my-vector 4))
; my-vector is unchanged, new-vector is [1 2 3 4]
(println new-vector)
; Using conj with a Set
(def my-set #{2 3})
(def new-set (conj my-set 1))
; my-set is unchanged, new-set is #{1 2 3}
(println new-set)
1
is added at the front.4
is added at the end.1
is added to the set if it’s not already present.assoc
(Add or update key-value pairs in a map)The assoc
function is used to add or update key-value pairs in a map. If the key already exists, it updates the value associated with that key. If the key does not exist, it adds the key-value pair to the map.
assoc
with Maps; Using assoc to add or update key-value pairs in a Map
(def my-map {:name "Alice" :age 30})
(def new-map (assoc my-map :age 31 :city "New York"))
; my-map is unchanged, new-map is {:name "Alice", :age 31, :city "New York"}
(println new-map)
:age
key is updated with the new value 31
.:city
key-value pair is added to the map.dissoc
(Remove key-value pairs from a map)The dissoc
function removes one or more key-value pairs from a map. It returns a new map with the specified keys removed.
dissoc
with Maps; Using dissoc to remove key-value pairs from a Map
(def my-map {:name "Alice" :age 30 :city "New York"})
(def new-map (dissoc my-map :age :city))
; my-map is unchanged, new-map is {:name "Alice"}
(println new-map)
:age
and :city
are removed from the map.conj
, assoc
, and dissoc
Function | Description | Example Usage |
---|---|---|
conj | Adds an element to a collection (front for lists, end for vectors, and to the set) | (conj [1 2] 3) → [1 2 3] , (conj #{1 2} 3) → #{1 2 3} |
assoc | Adds or updates key-value pairs in a map | (assoc {:a 1} :b 2) → {:a 1 :b 2} |
dissoc | Removes one or more key-value pairs from a map | (dissoc {:a 1 :b 2} :b) → {:a 1} |
conj
modifies the collection by adding elements at the correct position based on the type (beginning for lists, end for vectors).assoc
is specifically used to add or modify key-value pairs in a map.dissoc
removes key-value pairs from a map.These functions return a new collection and do not mutate the original collection, which is a core principle of functional programming and immutability in Clojure.
common.read_more